package org.jiandong.app.apprebotshutdown;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.bt_reboot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*PowerManager pm = (PowerManager) MainActivity.this.getSystemService(Context.POWER_SERVICE);
                pm.reboot("系统重启中");*/
                ((TextView) findViewById(R.id.bt_shutdown)).setText(modifyStrColor("hello world", "0 w", android.R.color.holo_red_dark));
            }
        });
        findViewById(R.id.bt_shutdown).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*try {

                    //获得ServiceManager类
                    Class<?> ServiceManager = Class.forName("android.os.ServiceManager");

                    //获得ServiceManager的getService方法
                    Method getService = ServiceManager.getMethod("getService", java.lang.String.class);

                    //调用getService获取RemoteService
                    Object oRemoteService = getService.invoke(null, Context.POWER_SERVICE);

                    //获得IPowerManager.Stub类
                    Class<?> cStub = Class.forName("android.os.IPowerManager$Stub");
                    //获得asInterface方法
                    Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
                    //调用asInterface方法获取IPowerManager对象
                    Object oIPowerManager = asInterface.invoke(null, oRemoteService);
                    //获得shutdown()方法
                    Method shutdown = oIPowerManager.getClass().getMethod("shutdown", boolean.class, boolean.class);
                    //调用shutdown()方法
                    shutdown.invoke(oIPowerManager, false, true);

                } catch (Exception e) {
                    Log.e("MainActivity", e.toString(), e);
                }*/

            }
        });
    }

    /**
     * change a part of string color.
     *
     * @param string    whole string.
     * @param subString the sub string need modify color.
     * @param color     the the color you want the sub string display. can get like
     *                  {@link Context#getResources()}.getColor(R.color.xxx) or use
     *                  the system {@link Color}.xxx;
     * @return a spannableStringBuilder(a string after modify substring color).
     */
    public static SpannableStringBuilder modifyStrColor(String string, String subString, int color) {
        String tipRed = String.format(string, subString);
        int index[] = new int[1];
        index[0] = tipRed.indexOf(subString);
        SpannableStringBuilder style = new SpannableStringBuilder(tipRed);
        style.setSpan(new ForegroundColorSpan(color), index[0],
                index[0] + subString.length(),
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        return style;
    }
}